package com.bbcloud.controller.portal;

import com.bbcloud.common.Const;
import com.bbcloud.common.ServerResponse;
import com.bbcloud.pojo.User;
import com.bbcloud.service.IUserService;
import com.bbcloud.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by liuzhu on 2018/11/2.
 */
@Controller
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private IUserService iUserService;

    /**
     * 用户登录
     * @param username  邮箱
     * @param password  密码
     * @return  用户对象和token
     */
    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<UserVo> login(String username, String password, HttpSession session){

        ServerResponse<UserVo>response = iUserService.login(username,password);
        if (response.isSuccess()){
                session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return  response;
    }


    /**
     * 注册新用户
     * @param username  用户名
     * @param validCode 验证码
     * @param password  密码
     * @param repassword    重复密码
     * @return
     */
    @RequestMapping(value = "reigster.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse register(String username,String validCode,String password,String repassword){
        return iUserService.register(username,validCode,password,repassword);
    }


    /**
     * 获取验证码
     * @param username
     * @return
     */
    @RequestMapping(value = "validcode.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getValidCode(String username){
        return iUserService.getValidCode(username);
    }

    /**
     * 获取用户信息
     * @return
     */
    @RequestMapping(value = "get_user_info.do",method= RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> getUserInfo(String username, String token){
        return iUserService.getUserInfo(username,token);
    }

    /**
     * 重置密码-忘记密码
     * @param username
     * @param passwordNew
     * @param validCode
     * @return
     */
    @RequestMapping(value = "forget_reset_password.do",method= RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> forgetResetPassword(String username,String password,String validCode){
        return iUserService.forgetResetPassword(username,password,validCode);
    }
}





























